﻿using System;
using MaLu.Common;
using System.Data;
using MaLu.BusiLogic;
using Wicresoft.Framework.Organization;
using MaLu.Data;

namespace MaluJg.web.Groups
{
    public partial class GBusiList : GBusiPage
    {

        public string SearchClauseItem
        {
            get
            {
                string where = "1=1";

                if (IsBusiAdmin)
                {
                   
                }
                else if (IsBusiSubAdmin)
                {
                    where += " and DepartmentID ='" + DepartmentID + "'";
                }
                else
                {
                    where += " and Creator ='" + CurrentUser.Name + "'";
                }

                if (tbName.Text.Trim() != string.Empty)
                {
                    where += " and GroupName like '%" + tbName.Text.Trim().Replace("'", "''") + "%'";

                }
                if (tbStart.Text.Trim() != string.Empty)
                {
                    where += " and CreateTime >='" + tbStart.Text.Trim().Replace("'", "''") + "'";

                }
                if (tbEnd.Text.Trim() != string.Empty)
                {
                    where += " and CreateTime <= '" + DateTime.Parse(tbEnd.Text.Trim()).AddDays(1).ToString("yyyy-MM-dd HH:mm:ss") + "'";

                }
                return where;
            }
             
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "组务列表";




            if (!IsPostBack)
            {
                this.Order.OrderColumn = "CreateTime";
                this.Order.OrderDirection = "DESC";
                Department mainD = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
                string[] ids = mainD.IDPath.Split('/');
                if (ids.Length >= 4)
                    DepartmentID = new Guid(ids[3]);
                else
                    DepartmentID = new Guid(ids[ids.Length - 1]);


               

            }

        }

        protected override void Pager_PageChanged(object sender, EventArgs e)
        {
            BindData();
            tbIDs.Text = string.Empty;
            base.Pager_PageChanged(sender, e);
        }

        protected override void Order_OrderChanged(object sender, EventArgs e)
        {
            BindData();
            base.Order_OrderChanged(sender, e);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {

            Pager.PageIndex = 0;

            if (tbIDs.Text.Trim() != string.Empty)
            {
                string sql = string.Format("delete from [GroupBusiness] where id in ('{0}');delete from [GroupFinance] where GroupRecordID in ('{0}');", tbIDs.Text.Trim().Substring(1).Replace(",", "','"));

                SqlHelper.Instance.ExecuteNonQuery(Provider, sql);
                tbIDs.Text = string.Empty;
            }

            BindData();
        }

        private void BindData()
        {
            int recordCount = 0;
            DataTable DTMyProposal = BLCommon.GetPagingList(Provider, "ID,GroupName,CreateTime,ModifyTime", "GroupBusiness", SearchClauseItem, "CreateTime desc", Pager.PageIndex + 1, Pager.PageSize, false, 0, ref recordCount);
            Pager.RecordCount = recordCount;
            rptBusi.DataSource = DTMyProposal;
            rptBusi.DataBind();
        }

        protected void btnAdvancedSearch_Click(object sender, EventArgs e)
        {
            
            BindData();
        }
    }
}
